﻿using CDV.IRMS.WebAPI.TaskExecution.Data;
using System;
using System.Net.Http;

namespace CDV.IRMS.WebAPI.TaskExecution.Interface.Impl
{
    /// <summary>
    /// 矩阵控制实现类
    /// </summary>
    public class MatrixControlServiceImpl : IMatrixControlService
    {
        /// <summary>
        /// 矩阵控制接口API
        /// </summary>
        public RESTfulAPI RESTfulAPI { get; set; }

        /// <summary>
        /// 矩阵切换
        /// </summary>
        /// <param name="srcPort">源端口</param>
        /// <param name="destPort">目的端口</param>
        /// <param name="matrixID">矩阵ID</param>
        /// <returns>true:切换成功</returns>
        public (bool, string) Take(int srcPort, int destPort, int? matrixID)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(RESTfulAPI.BaseURL);
                var actionUri = string.Format(RESTfulAPI.APIMap["Take"], matrixID, 1, destPort, srcPort);
                var response = httpClient.GetAsync(actionUri, HttpCompletionOption.ResponseHeadersRead);
                if (response.Result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var result = response.Result.Content.ReadAsStringAsync();
                    return (result.Result == "true", string.Empty);
                }

                return (false, $"矩阵[矩阵ID:{matrixID}, 源端口:{srcPort}--->目的端口:{destPort}]切换失败！");
            }
        }

        /// <summary>
        /// 锁定矩阵目的端口
        /// </summary>
        /// <param name="destPort">目的端口</param>
        /// <param name="matrixID">矩阵ID</param>
        /// <returns>true:锁定成功</returns>
        public (bool, string) Lock(int destPort, int? matrixID = null)
        {
            return LockOrUnlock("Lock", destPort, matrixID);
        }

        /// <summary>
        /// 矩阵目的端口解锁
        /// </summary>
        /// <param name="destPort">目的端口</param>
        /// <param name="matrixID">矩阵ID</param>
        /// <returns>true:解锁成功</returns>
        public (bool, string) UnLock(int destPort, int? matrixID = null)
        {
            return LockOrUnlock("Unlock", destPort, matrixID);
        }

        /// <summary>
        /// 矩阵目的端口锁定/解锁
        /// </summary>
        /// <param name="action">Lock or Unlock</param>
        /// <param name="destPort">目的端口</param>
        /// <param name="matrixID">矩阵ID</param>
        /// <returns>true:锁定/解锁成功</returns>
        private (bool, string) LockOrUnlock(string action, int destPort, int? matrixID)
        {
            if (!matrixID.HasValue)
            {
                return (false, "矩阵ID为空！");
            }

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(RESTfulAPI.BaseURL);
                var actionUri = string.Format(RESTfulAPI.APIMap[action], matrixID.Value, destPort);
                var response = httpClient.GetAsync(actionUri, HttpCompletionOption.ResponseHeadersRead);
                if (response.Result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var result = response.Result.Content.ReadAsStringAsync();
                    return (result.Result == "true", string.Empty);
                }

                return (false, $"{action}矩阵[矩阵ID:{matrixID}]的目的端口[{destPort}]失败！");
            }
        }
    }
}
